const router = require('koa-router')();
const user = require('./admin/user.js');
const banner = require('./admin/banner.js');
const login = require('./admin/login.js');

//配置中间件 获取url地址
/* router.use(async (ctx,next)=>{
    ctx.state.__HOST__ = 'http://' + ctx.request.header.host;
    console.log(ctx.state.__HOST__)
    await next();
})
 */

router.use(async (ctx,next)=>{
    if(ctx.session.userinfo){
        await next();
    }else{
        if(ctx.url == '/admin/login' || ctx.url == '/admin/login/doLogin'){
            await next();
        }else{
            ctx.redirect('/admin/login')
        }
    }
})


router.get('/',async (ctx)=>{
    await ctx.render('admin/index')
})

//配置admin的子路由 层级路由
router.use('/user',user);
router.use('/banner',banner);
router.use('/login',login);

module.exports = router.routes();